package com.kx.project.exam.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kx.common.constant.UserConstants;
import com.kx.common.utils.SecurityUtils;
import com.kx.common.utils.poi.ExcelUtil;
import com.kx.platform.aspectj.lang.annotation.Log;
import com.kx.platform.aspectj.lang.enums.BusinessType;
import com.kx.platform.web.controller.BaseController;
import com.kx.platform.web.domain.AjaxResult;
import com.kx.platform.web.page.TableDataInfo;
import com.kx.project.exam.domain.Question;
import com.kx.project.exam.service.QuestionService;

/**
 * 题目 controller组件
 * 
 * @author kx  
 */
@RestController
@RequestMapping("/exam/question")
public class QuestionController extends BaseController
{
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @GetMapping("/list")
    public TableDataInfo listByPage(Question question) {
        startPage();
        List<Question> list = questionService.listByPage(question);
        return getDataTable(list);
    }

    /**
     * 根据id查询
     */
    @PreAuthorize("@ss.hasPermi('exam:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.success(questionService.getById(id));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:add')")
    @Log(title = "题目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Question question) {
        if (UserConstants.NOT_UNIQUE.equals(questionService.checkUnique(question))) {
            return AjaxResult.error("新增'" + question.getQuestionName() + "'失败，数据已存在");
        }
        question.setCreateBy(SecurityUtils.getUsername());
        return toAjax(questionService.insert(question));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:edit')")
    @Log(title = "题目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Question question) {
        if (UserConstants.NOT_UNIQUE.equals(questionService.checkUnique(question))) {
            return AjaxResult.error("修改参数'" + question.getQuestionName() + "'失败，数据已存在");
        }
        question.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(questionService.update(question));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:remove')")
    @Log(title = "题目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(questionService.removeByIds(ids));
    }

    /**
     * 导出题目
     * @param question
     * @return
     */
    @Log(title = "题目管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('exam:question:export')")
    @GetMapping("/export")
    public AjaxResult export(Question question) {
        List<Question> list = questionService.listByPage(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        return util.exportExcel(list, "数据页");
    }
}
